package com.monkey.util;

import com.monkey.model.monkey.MonkeyData;
import com.monkey.model.monkey.MonkeyTradeData;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

/**
 * desc: auth: chenxiaobin data: 2018/1/15.
 */
@Component
public class HelpUtil {

	public boolean getListWithMonkeyData(List<MonkeyData> l, int offset, int num) {
		int size = l.size();
		if (offset >= size) {
			return false;
		}
		if (offset + num > size) {
			num = size - offset;
		}
		List<MonkeyData> tmp = new ArrayList<>();
		for (int i = 0; i < num; ++i) {
			tmp.add(l.get(offset + i));
		}
		l.clear();
		l.addAll(tmp);
		return true;
	}

	public boolean getListWithMonkeyTradeData(List<MonkeyTradeData> l, int offset, int num) {
		int size = l.size();
		if (offset >= size) {
			return false;
		}
		if (offset + num > size) {
			num = size - offset;
		}
		List<MonkeyTradeData> tmp = new ArrayList<>();
		for (int i = 0; i < num; ++i) {
			tmp.add(l.get(offset + i));
		}
		l.clear();
		l.addAll(tmp);
		return true;
	}

	public String getIp(HttpServletRequest request) {
		String ip = request.getHeader("x-forwarded-for");
		if (null == ip) {
			return request.getRemoteAddr();
		}
		return ip;
	}

	public List<Integer> splitToIntegerList(String str, String regex) {
		List<Integer> resultList = new ArrayList<>();
		if (null != str) {
			String[] items = str.trim().split(regex);
			for (String item : items) {
				try {
					resultList.add(Integer.parseInt(item));
				} catch (NumberFormatException e) {

				}
			}
		}
		return resultList;
	}
	
	/*
     * 判断List中是否有重复的值
     */
    public boolean cheakIsRepeat(List<Integer> list) {
        HashSet<Integer> hashSet = new HashSet<Integer>();
        for (Integer item : list) {
            hashSet.add(item);
        }
        if (hashSet.size() == list.size()) {
            return true;
        } else {
            return false;
        }
    }
}
